package com.house.java.biz;

import java.util.ArrayList;
import java.util.List;

import com.house.java.dao.DaoFactory;
import com.house.java.dao.HouseSurroundDAO;
import com.house.java.dao.NewHouseDAO;
import com.house.java.dao.PriceHistoryDAO;
import com.house.java.dao.impl.HouseSurroundDaoImpl;
import com.house.java.dao.impl.NewHouseDaoImpl;
import com.house.java.dao.impl.PriceHistoryDaoImpl;
import com.house.java.model.HistoryPrice;
import com.house.java.model.HouseSurround;
import com.house.java.model.NewHouse;
import com.house.java.model.Paging;

public class NewHouseBusiness {
	/**
	 * 根据查询条件获取新楼盘的数据
	 * @param minPrice  最小均价
	 * @param maxPrice	最大均价
	 * @param wuyeCategory	物业类别
	 * @param locationDistrict	城市的区CODE
	 * @param locationCityCode	城市的CODE
	 * @param locationProvinceCode	省份CODE
	 * @param locationCountryCode	国家CODE
	   	分页参数
	 * @param currentPage  当前多少页	
	 * @param pageSize		每页大小
	 * @return
	 */
	public List<NewHouse> queryNewHouses(Integer minPrice,Integer maxPrice,Integer wuyeCategory,
			Integer locationDistrict,Integer locationCityCode,Integer locationProvinceCode,
			Integer locationCountryCode,Integer currentPage,Integer pageSize,Long id){
		List<NewHouse> list = new ArrayList<NewHouse>();
		NewHouseDAO dao = (NewHouseDaoImpl)DaoFactory.getDaoImpl(NewHouseDAO.class);
		Paging paging = new Paging();
		if(currentPage!=null){
			paging.setCurrentPage(currentPage);
			if(null != pageSize){
				paging.setPageSize(pageSize);
			}else {
				paging.setPageSize(10);
			}
		}else {
			paging.setCurrentPage(1);
			paging.setPageSize(10);
		}
		list = dao.queryNewHouseByCondition(maxPrice, minPrice, paging, wuyeCategory, locationCountryCode, 
				locationProvinceCode, locationCityCode, locationDistrict,id);
		
		return list;
	}
	
	
	public NewHouse queryById(Long id){
		NewHouseDAO dao = (NewHouseDaoImpl)DaoFactory.getDaoImpl(NewHouseDAO.class);
		List<NewHouse> list = dao.queryNewHouseByCondition(id);
		if(null != list && list.size() > 0){
			return list.get(0);
		}
		return null;
	}
	/**
	 * 添加NewHouse对象
	 * @param house  
	 * @return
	 */
	public boolean addNewHouse(NewHouse house){
		if(null != house.getId()){
			return update(house);
		}else {
			addHouseSurrent(house.getSurrounding());
			if(null != house.getSurrounding().getId()){
				house.setHouseSurroundId(house.getSurrounding().getId());
//			boolean result = addHistoryPrice(house.getHistoryPrice());
//			if(!result){
//				return false;
//			}
			}else {
				return false;
			}
			NewHouseDAO dao = (NewHouseDaoImpl)DaoFactory.getDaoImpl(NewHouseDAO.class);
			dao.insertNewHouse(house);
			if(null != house.getId()){
				return false;
			}
			return false;
		}
	}
	
	
	public boolean update(NewHouse house){
		NewHouseDAO dao = (NewHouseDaoImpl)DaoFactory.getDaoImpl(NewHouseDAO.class);
		List<NewHouse> list = dao.queryNewHouseByCondition(null, null, null, null, null, null, null, null, house.getId());
		if(null != list && list.size() == 1){
			HouseSurroundDAO houseSurroundDAO = (HouseSurroundDaoImpl)DaoFactory.getDaoImpl(HouseSurroundDAO.class);
			int result = -1;
			// 如果传过来的NewHouse 关联的     HouseSurrendId  有值，就删除，如果没有值，就不删除，直接绑定到原来对象上
			if(null != house.getSurrounding()){
				int surrendResult = houseSurroundDAO.delete(house.getHouseSurroundId());
				if(surrendResult > 0){
					result = dao.deleteNewHouse(house.getId());
				}
			}else {
				house.setHouseSurroundId(list.get(0).getHouseSurroundId());
			}
			if(result > 0){
				house.setId(null);
				addNewHouse(house);
				return true;
			}else {
				return false;
			}
		}
		return false;
	}
	/**
	 * 添加 HouseSurrend
	 * @param surround
	 */
	public void addHouseSurrent(HouseSurround surround){
		HouseSurroundDAO dao = (HouseSurroundDaoImpl)DaoFactory.getDaoImpl(HouseSurroundDAO.class);
		dao.insertHouseSurround(surround);
	}
	
	/**
	 * 添加 HistoryPrice
	 * @param historyPrices
	 */
	public boolean addHistoryPrice(List<HistoryPrice> historyPrices){
		PriceHistoryDAO dao = (PriceHistoryDaoImpl)DaoFactory.getDaoImpl(PriceHistoryDAO.class);
		boolean result = false;
		for (HistoryPrice historyPrice : historyPrices) {
			dao.insertPriceHistory(historyPrice);
			if(null != historyPrice.getId()){
				result=true;
			}else {
				result=false;
			}
		}
		return result;
	}
	
	public boolean delete(Long id){
		HouseSurroundDAO dao = (HouseSurroundDaoImpl)DaoFactory.getDaoImpl(HouseSurroundDAO.class);
		int result = dao.delete(id);
		if(result > 0){
			return true;
		}else {
			return false;
		}
	}
	

}
